using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace AudioStudio
{
    /// <summary>
    /// Button���ʱ����audioEvent
    /// </summary>
    [AddComponentMenu("AudioStudio/ButtonSound")]
    public class ButtonSound : MonoBehaviour
    {
        public AudioEvent[] audioEvents;
        public AudioEvent mouseinEvent;

        void Start()
        {
            Button button = GetComponent<Button>();
            //UIButton btn = GetComponent<UIButton>();
            if (button != null && button.onClick != null)
            {
                button.onClick.AddListener(PlaySound);
                EventTrigger trigger = button.gameObject.GetComponent<EventTrigger>();
                if (trigger != null)
                {
                    EventTrigger.Entry entry = new EventTrigger.Entry();
                    entry.eventID = EventTriggerType.PointerEnter;
                    entry.callback = new EventTrigger.TriggerEvent();
                    entry.callback.AddListener(OnPointEnter);
                    trigger.triggers.Add(entry);
                }
            }
//             else if (btn != null && btn.onClick  != null)
//             {
//                 btn.onClick.Add(new EventDelegate(() =>
//                 {
//                     PlaySound();
//                 }
//                 ));
//                 UIEventTrigger trigger = btn.gameObject.GetComponent<UIEventTrigger>();
//                 if (trigger != null)
//                 {
//                     trigger.onHoverOver.Add(new EventDelegate(() =>
//                     {
//                         OnPointEnter(null);
//                     }
//                     ));
//                 }
        }

        public void PlaySound()
        {
            if(AkSoundEngine.IsInitialized())
            {
                AudioManager.Instance.PostEvents(null, audioEvents);
            }
        }

        public void OnPointEnter(BaseEventData pointData)
        {
            if (AkSoundEngine.IsInitialized() && mouseinEvent != null)
            {
                mouseinEvent.Post();
            }
        }
    }
}

